using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO.WIX
{
    public class WixMakeDAO : Abstracts.AbstractGenericDAO<WixMake, System.Nullable<int>>,
        Interfaces.WIX.IWixMakeDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "make_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie pojazdu.
        /// </summary>
        /// <param name="vehicleId">pojazd</param>
        /// <returns>rekordy o zadanym pojezdzie lub null, gdy nie ma</returns>
        public IQueryable<WixMake> FindByVehicle(int vehicleId)
        {
            if (vehicleId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixMake> makes = this._cmsEntitiesDataContext.WixMakes.Where(
                    make => make.vehicle_id.Equals(vehicleId) 
                        ).OrderBy(make => make.make_name);
                if (!(makes.Any()))
                {
                    return null;
                }   //if
                return makes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie pojazdu.
        /// </summary>
        /// <param name="vehicleId">pojazd</param>
        /// <returns>rekordy o zadanym pojezdzie lub null, gdy nie ma</returns>
        public IQueryable<WixMake> FindByVehicle(int vehicleId, int secondVehicleId)
        {
            if (vehicleId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixMake> makes = this._cmsEntitiesDataContext.WixMakes.Where(
                    make => make.vehicle_id.Equals(vehicleId) || make.vehicle_id.Equals(secondVehicleId)
                        ).OrderBy(make => make.make_name);
                if (!(makes.Any()))
                {
                    return null;
                }   //if
                return makes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie filtru.
        /// </summary>
        /// <param name="filterId">model samochodu</param>
        /// <returns>rekordy o zadanym filtrze lub null, gdy nie ma</returns>
        public IQueryable<WixMake> FindByFilter(int filterId)
        {
            if (filterId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<WixMake> makes = this._cmsEntitiesDataContext.WixMakes.Where(
                    make => this._cmsEntitiesDataContext.WixModels.Where(mode =>
                        this._cmsEntitiesDataContext.WixEngineModels.Where(enmo =>
                        this._cmsEntitiesDataContext.WixFilterEngines.Where(fien =>
                        fien.filter_id.Equals(filterId)).Select(fe => fe.engine_id).Contains(enmo.engine_id)
                        ).Select(em => em.model_id).Contains(mode.model_id)
                        ).Select(mo => mo.make_id).Contains(make.make_id)
                        );                       
                        
                if (!(makes.Any()))
                {
                    return null;
                }   //if
                return makes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }
    }
}
